import requests
from lxml import etree
import re
import os
if not os.path.exists('高清壁纸'):
    os.mkdir('高清壁纸')
# 高清壁纸
page = 1
count = 1
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}
while True:
    print(f'正在下载第{page}页')
    # url = 'https://hdqwalls.com/'
    url = f'https://hdqwalls.com/latest-wallpapers/page/{page}'
    response = requests.get(url=url, headers=headers)
    text = response.text
    # html = etree.HTML(text)
    # 缩略图
    # thumbnail = html.xpath('/html/body/div[1]/div[2]/div[*]/a[2]/img/@src')
    thumbnail = re.findall("src=\'(.*?)\' class=\'thumbnail img-responsive custom_width", text)
    print(len(thumbnail))
    for i in thumbnail:
        print(i)
        thumbnail_url = i.replace('thumb/', '')
        print(thumbnail_url)
        thumbnail_response = requests.get(thumbnail_url, headers=headers)
        with open(f'高清壁纸/{count}.jpg', 'wb') as f:
            f.write(thumbnail_response.content)
        count += 1
    page += 1








# <img width="300px" height="200px" title="Batman Violet Vengeance Wallpaper" alt="Batman Violet Vengeance Wallpaper" src="https://images.hdqwalls.com/wallpapers/thumb/batman-violet-vengeance-xx.jpg" class="thumbnail img-responsive custom_width">































